﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 易容酒店管理系统
{
    public partial class Log_In : Form
    {
        public Log_In()
        {
            InitializeComponent();
        }

        private string User = "";
        private string Pass = "";
        private string ID = "";
        private string Type = "";
        private bool judge = false;

        private void Log_In_Load(object sender, EventArgs e)
        {
            txtID.BackColor = Color.FromArgb(102, 102, 102);
            txtPassWord.BackColor = Color.FromArgb(102, 102, 102);
            txtUserName.BackColor = Color.FromArgb(102, 102, 102);
            panel1.BackColor = Color.FromArgb(254, 133, 77);
        }

        private void pitCloss_Click(object sender, EventArgs e)
        {
            this.Close();
        }
       
        private void pictLog_in_Click(object sender, EventArgs e)
        {
            
            string Link = "server=.;Database=Management_system;integrated security=SSPI;";
            using (SqlConnection con = new SqlConnection(Link))
            {
                string Sql = "select * from StaffInfo where staffID='" + txtID.Text + "'or StaffName='" + txtUserName.Text + "';";
                using (SqlCommand cmd = new SqlCommand(Sql, con))
                {
                    con.Open();
                    SqlDataReader red = cmd.ExecuteReader();
                    if (red.HasRows)
                    {
                         red.Read();
                         ID = (string)red[0];
                         User = (string)red[1];
                         Pass = (string)red[2];
                         Type = (string)red[3];
                    }
                }
            }
            if (txtPassWord.Text != Pass)
            {
                MessageBox.Show("用户名或密码错误！", "系统提示");
            }
            else
            {
               // MessageBox.Show("登录成功！", "系统提示");
                judge = true;
                Method.Bool = judge;
                Method.UserName = User;
                Method.type = Type;
                this.Close();
            }
        }
        #region 自动获取员工ID和名字
        private void txtID_Leave(object sender, EventArgs e)
        {
            string Link = "server=.;Database=Management_system;integrated security=SSPI;";
            using (SqlConnection con = new SqlConnection(Link))
            {
                string name = "";
                string id = "";
                string Sql = "select staffID ,StaffName from StaffInfo where staffID='" + txtID.Text + "'or StaffName='" + txtUserName.Text + "';";
                using (SqlCommand cmd = new SqlCommand(Sql, con))
                {
                    con.Open();
                    SqlDataReader red = cmd.ExecuteReader();
                    if (red.HasRows)
                    {
                        red.Read();
                        id = (string)red[0];
                        name= (string)red[1];
                    }
                }
                if (txtUserName.Text == "")
                {
                    txtUserName.Text = name;
                }
                
            }
        }

        private void txtUserName_Leave(object sender, EventArgs e)
        {
            string Link = "server=.;Database=Management_system;integrated security=SSPI;";
            using (SqlConnection con = new SqlConnection(Link))
            {
                string name = "";
                string id = "";
                string Sql = "select staffID ,StaffName from StaffInfo where staffID='" + txtID.Text + "'or StaffName='" + txtUserName.Text + "';";
                using (SqlCommand cmd = new SqlCommand(Sql, con))
                {
                    con.Open();
                    SqlDataReader red = cmd.ExecuteReader();
                    if (red.HasRows)
                    {
                        red.Read();
                        id = (string)red[0];
                        name = (string)red[1];
                    }
                }
                if (txtID.Text == "")
                {
                    txtID.Text = id;
                }

            }
        }
        #endregion
    }
}
